package com.holly.unit.system.modular.user.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.holly.unit.auth.api.context.LoginContext;
import com.holly.unit.auth.api.exception.AuthException;
import com.holly.unit.auth.api.exception.enums.AuthExceptionEnum;
import com.holly.unit.auth.api.pojo.login.LoginUser;
import com.holly.unit.cache.api.CacheOperatorApi;
import com.holly.unit.config.api.context.ConfigContext;
import com.holly.unit.system.api.NoticeServiceApi;
import com.holly.unit.system.api.constants.SystemCachesConstants;
import com.holly.unit.system.api.constants.SystemConstants;
import com.holly.unit.system.api.pojo.notice.SysNoticeRequest;
import com.holly.unit.system.api.pojo.user.HisUserPasswordDTO;
import com.holly.unit.system.api.pojo.user.SysUserDTO;
import com.holly.unit.system.api.pojo.user.SysUserOrgDTO;
import com.holly.unit.system.api.pojo.user.request.SysUserRequest;
import com.holly.unit.system.modular.user.cache.SysHisPasswordCache;
import com.holly.unit.system.modular.user.entity.SysHisUserPassword;
import com.holly.unit.system.modular.user.mapper.SysHisUserPasswordMapper;
import com.holly.unit.system.modular.user.service.SysHisUserPasswordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 密码过期策略
 */
@Service
@Slf4j
public class SysHisUserPasswordServiceImpl extends ServiceImpl<SysHisUserPasswordMapper, SysHisUserPassword> implements SysHisUserPasswordService {

    @Autowired
    private SysHisUserPasswordMapper hisUserPasswordMapper;

//    @Resource
//    private SysHisPasswordCache cacheHisPassWordApi;

    @Resource(name = "cacheHisPassWordApi")
    private CacheOperatorApi<HisUserPasswordDTO> cacheHisPassWordApi;

    @Autowired
    private NoticeServiceApi noticeServiceApi;
    private Integer EXPIRE_REMIND = 90;

    private static final String CACHE_PASSWORD_LAST = "password_last:";

    @Override
    public SysHisUserPassword addPassword(Long userId, SysHisUserPassword hisUserPassword) {
        this.baseMapper.insert(hisUserPassword);
        cacheHisPassWordApi.remove(String.valueOf(userId));
        return hisUserPassword;
    }

    /**
     * @param userId 账号有效期
     * @return
     */
    @Override
    public HisUserPasswordDTO findPasswordExpiringUser(Long userId) {
        int indate = ConfigContext.me().getSysConfigValueWithDefault(SystemConstants.PW_TIME_OUT, Integer.class, 90);
        if(indate >90 || indate <1) indate = 90;
        int outdate = ConfigContext.me().getSysConfigValueWithDefault(SystemConstants.PW_TIME_NOTICE, Integer.class, 7);
        if (outdate > 90 || outdate < 1) outdate = 1;
        HisUserPasswordDTO cacheHisPassword = cacheHisPassWordApi.get(String.valueOf(userId));
        if (cacheHisPassword != null)
        {
            //密码有效期配置天数如果变动，则删除缓存
            if(cacheHisPassword.getIndate() != indate)
            {
                cacheHisPassWordApi.remove(String.valueOf(userId));
            }else
            {
                return cacheHisPassword;
            }
        }
        // 过期时间**********************有效时间………7天--------------------------90天-------------------------当前时间
        Long indateTime = indate * 1000L * 60L * 60L * 24L;
        Long currentTime = System.currentTimeMillis();
        Long begin = currentTime - indateTime;
        // 从历史密码表取过期数据
        QueryWrapper<SysHisUserPassword> wrapper = new QueryWrapper<>();
        wrapper.select("user_id,create_time,type");
        wrapper.eq("user_id", userId);
        wrapper.gt("create_time",begin);
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 1");
        SysHisUserPassword sysHisUserPassword = hisUserPasswordMapper.selectOne(wrapper);
        if(sysHisUserPassword == null) return null;
        HisUserPasswordDTO hisUserPasswordDTO = new HisUserPasswordDTO();
        BeanUtils.copyProperties(sysHisUserPassword, hisUserPasswordDTO);
        hisUserPasswordDTO.setIndate(indate);
        cacheHisPassWordApi.put( String.valueOf(userId), hisUserPasswordDTO,indate*24*60*60L);
        return hisUserPasswordDTO;
    }
}
